CMAKE_MINIMUM_REQUIRED(VERSION 3.16)
PROJECT(air-link-asn-wrapper)

SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC -Werror=return-type -DNDEBUG")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -Werror=return-type -DNDEBUG")

SET(CMAKE_FIND_PACKAGE_PREFER_CONFIG TRUE)

SET(GeographicLib_USE_STATIC_LIBS TRUE)
FIND_PACKAGE(GeographicLib REQUIRED)
FIND_PACKAGE(glog REQUIRED)
FIND_PACKAGE(pugixml REQUIRED)
FIND_PACKAGE(protobuf REQUIRED)

### PROTO
FILE(GLOB proto_files "proto/*.proto")
PROTOBUF_GENERATE(
        APPEND_PATH
        LANGUAGE cpp
        OUT_VAR pb_gen
        PROTOC_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}
        PROTOS ${proto_files}
)
ADD_LIBRARY(${PROJECT_NAME}-grpc_pb STATIC ${pb_gen})
TARGET_LINK_LIBRARIES(${PROJECT_NAME}-grpc_pb
        protobuf::libprotobuf)
TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME}-grpc_pb
        PUBLIC ${CMAKE_CURRENT_BINARY_DIR})

### Modules
FOREACH (ASN_MODULE_NAME
        asn_3
        asn_2
        )
  SET(ASN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/${ASN_MODULE_NAME})
  SET(ASN_GEN_DIR ${CMAKE_CURRENT_BINARY_DIR}/${ASN_MODULE_NAME})
  FILE(MAKE_DIRECTORY ${ASN_GEN_DIR})
  EXECUTE_PROCESS(
          COMMAND
          bash ${CMAKE_CURRENT_SOURCE_DIR}/scripts/genasn.sh "${ASN_SRC_DIR}"
          WORKING_DIRECTORY ${ASN_GEN_DIR}
          RESULT_VARIABLE script_res
  )
  IF (NOT ${script_res} EQUAL 0)
    MESSAGE(FATAL_ERROR "Failed to gen asn.")
  ENDIF ()
  UNSET(__asn_srcs)
  AUX_SOURCE_DIRECTORY(${ASN_GEN_DIR}/asncodec __asn_srcs)
  ADD_LIBRARY(${PROJECT_NAME}-${ASN_MODULE_NAME}-asncodec STATIC
          ${__asn_srcs}
          )
  TARGET_COMPILE_OPTIONS(${PROJECT_NAME}-${ASN_MODULE_NAME}-asncodec
          BEFORE PRIVATE
          -w
          )
  TARGET_INCLUDE_DIRECTORIES(${PROJECT_NAME}-${ASN_MODULE_NAME}-asncodec
          PUBLIC ${ASN_GEN_DIR}/asncodec
          )
  ### CONFIG FILES
  CONFIGURE_FILE(
          include/v2x-asn-msgs-module.h.in
          v2x-asn-msgs-${ASN_MODULE_NAME}.h
  )
  UNSET(__srcs)
  AUX_SOURCE_DIRECTORY(./src __srcs)
  SET(TGT_NAME ${PROJECT_NAME}-${ASN_MODULE_NAME})
  ADD_LIBRARY(${TGT_NAME} SHARED
          ${__srcs}
          )
  TARGET_COMPILE_OPTIONS(${TGT_NAME}
          PRIVATE
          -fno-rtti
          -fno-weak
          )
  TARGET_COMPILE_DEFINITIONS(${TGT_NAME}
          PRIVATE
          CONFIG_ADAPTER_MODULE_${ASN_MODULE_NAME}_ENABLE
          )
  SET_TARGET_PROPERTIES(${TGT_NAME} PROPERTIES
          CXX_VISIBILITY_PRESET hidden
          )
  TARGET_INCLUDE_DIRECTORIES(${TGT_NAME} PUBLIC
          include
          )
  TARGET_INCLUDE_DIRECTORIES(${TGT_NAME} PRIVATE
          ${CMAKE_CURRENT_BINARY_DIR}
          )
  TARGET_LINK_LIBRARIES(${TGT_NAME}
          PRIVATE
          -Wl,--whole-archive
          ${PROJECT_NAME}-${ASN_MODULE_NAME}-asncodec
          ${PROJECT_NAME}-grpc_pb
          -Wl,--no-whole-archive
          ${GeographicLib_LIBRARIES}
          pugixml::static
          glog::glog
          -Wl,-z,defs
          -Wl,--exclude-libs,ALL
          )
  ######### INSTALL
  INSTALL(TARGETS
          ${TGT_NAME}
          LIBRARY DESTINATION lib
          ARCHIVE DESTINATION lib
          RUNTIME DESTINATION /bin
          )
  INSTALL(FILES
          ${CMAKE_CURRENT_BINARY_DIR}/v2x-asn-msgs-${ASN_MODULE_NAME}.h
          DESTINATION include
          )
ENDFOREACH ()

######### INSTALL
INSTALL(FILES
        include/v2x-asn-msgs-common.h
        include/v2x-asn-msgs-adapter.hpp
        DESTINATION include
        )
INSTALL(FILES
        ${proto_files}
        DESTINATION proto
        )
